#include "UdpServer.hpp"
#include "Route.hpp"
#include "ThreadPool.hpp"
#include <functional>
#include <memory>
#include <iostream>

using namespace threadPoolModule;

using task_t = std::function<void()>;

std::string defaultFun(const std::string& str)
{
    LOG(LogLevel::DEBUG) << "client info:" << str;
    std::string ans = "success for send info:";
    return ans += str;
}

int main(int argc, char* args[])
{
    if (argc != 2)
    {
        std::cerr << "格式错误，正确格式: ./UdpServer port" << std::endl;
        return 1;
    }

    ENABLE_CONSOLE_LOG_STRATEGY();

    uint16_t port1 = std::stoi(args[1]); 

    Route route;

    ThreadPool<task_t>::getInstance()->start();

    auto tp = ThreadPool<task_t>::getInstance();
    
    UdpServer server(port1, [&route, &tp](int sockfd, const std::string& message, const InetAddr& client)->void
        { 
            task_t task = std::bind(&Route::messageRoute, &route, sockfd, message, client);
            tp->pushTask(task);
        });

    LOG(LogLevel::DEBUG) << "server init";  
    server.init();  
    LOG(LogLevel::DEBUG) << "server start";    
    server.start();

    ThreadPool<task_t>::getInstance()->stop();

    ThreadPool<task_t>::getInstance()->join();

    return 0;
}